#include <iostream>
using namespace std;
#include <vector>
#include <deque>


void process1(vector<int>& arr, vector<int>& ans, int W)
{
    static int L = 0;
    static int R = 0;
    deque<int> dq(0, 0);

    while (R - W + 1 <= 0)
    {
        while (!dq.empty() && arr[dq.back()] <= arr[R])
        {
            dq.pop_back();
        }

        dq.push_back(R);

        R++;
    }

    int N = static_cast<int>(arr.size());
    R = W - 1;
    while (R < N)
    {

        if (!dq.empty() && arr[dq.back()] <= arr[R])
        {
            dq.pop_back();
        }

        dq.push_back(R);

        while (L > dq.front())
        {
            dq.pop_front();
        }

        ans.push_back(arr[dq.front()]);
        L++;
        R++;
    }
}

void process11(vector<int>& arr , vector<int>& ans , int W)
{
  static int L = 0;
  static int R = 0;
  deque<int> dq(0 , 0);

  while(R - W + 1 < 0)
  {
    while (!dq.empty() && arr[dq.back()] <= arr[R])
    {
      dq.pop_back();
    }

    dq.push_back(R);
    
    R++;
  }

  int N = static_cast<int>(arr.size());
  R = W - 1;
  while (R < N)
  {
    ans.push_back(arr[dq.front()]);
    L++;
    R++;

    if (!dq.empty() && arr[dq.back()] <= arr[R])    
    {    
      dq.pop_back();    
    }               
                            
    dq.push_back(R);  

    while (L > dq.front())
    {
      dq.pop_front();
    }
  }
}


void process(vector<int>& arr, vector<int>& ans, int W) {
  deque<int> dq;

  for (int R = 0; R < W; R++) {
    while (!dq.empty() && arr[dq.back()] <= arr[R]) {
      dq.pop_back();
    }
    dq.push_back(R);
  }

  int N = static_cast<int>(arr.size());

  for (int R = W; R < N; R++) {
    ans.push_back(arr[dq.front()]);

    while (!dq.empty() && dq.front() <= R - W) {
      dq.pop_front();
    }

    while (!dq.empty() && arr[dq.back()] <= arr[R]) {
      dq.pop_back();
    }

    dq.push_back(R);
  }

  ans.push_back(arr[dq.front()]);
}

void process3(vector<int>& arr, vector<int>& ans, int W) {
  static int L = 0;
  static int R = 0;
  deque<int> dq;

  while (R - W + 1 < 0) {
    if (!dq.empty() && arr[dq.back()] <= arr[R]) {
      dq.pop_back();
    }
    dq.push_back(R);
    R++;
  }

  int N = static_cast<int>(arr.size());

  while (R < N) {
    if (!dq.empty() && arr[dq.back()] <= arr[R]) {
      dq.pop_back();
    }
    dq.push_back(R);

    while (L > dq.front()) {
      dq.pop_front();
    }

    ans.push_back(arr[dq.front()]);
    L++;
    R++;
  }
}



void process5(vector<int>& arr , vector<int>& ans , int W)
{
  deque<int> dq(0  , 0);

  for (int R = 0; R < W; R++)
  {
    while (!dq.empty() && arr[dq.front()] <= arr[R])
    {
      dq.pop_back();
    }

    dq.push_back(R);
  }

  int R = W - 1;
  int L = 0;
  int N = static_cast<int>(arr.size());
  while (R < N)
  {
    while (!dq.empty() && arr[dq.back()] <= arr[R])                                                                                                               
    {    
        dq.pop_back();    
    }    
      
    dq.push_back(R);    
     
    while (L > dq.front())    
    {    
       dq.pop_front();    
    }    
      
    ans.push_back(arr[dq.front()]);    
    L++;    
    R++;
  }
}
int main()
{
  vector<int> arr = {4 , 3, 5 , 4 , 3, 3, 6, 7};
  vector<int> ans(0 , 0);
  process5(arr , ans , 3);
  for (auto x : ans)
  {
    cout << x << " ";
  }
  cout << endl;
  return 0;
}
